from rest_framework.authentication import BaseAuthentication
from user import models
from utils.tooljwt import ToolJwt

tooljwt = ToolJwt()
class UserJWTAuthentication(BaseAuthentication):
    def authenticate(self, request):
        token = request.META.get('HTTP_AUTHORIZATION', None)
        if token:
            token = token.split(' ')[1]
            userdata=tooljwt.check_token(token)
            if userdata:
                userid= userdata['userid']
                user = models.Users.objects.filter(id=userid).first()
                return user,token
        else:
            return None,None
    def authenticate_header(self, request):
        return 'Bearer realm="api"'
